#!/bin/bash

# THIS VERSION IS BUGGED - IT NEEDS FIXES TO RUN, SORRY (changes in imagemagick tools syntax) #

# Release - 22/03/08 #

############################ SupTime Project v0.55 ###############################
# Hi, this is really-easy bash script which generates png image with your system #
# top and current uptime.                                                        #
##################################################################################
# Dear User - consider that this script have been written _FOR_PERSONAL_USE_.    #
# Please don't await anything amazing and excellent. Also my linux knowledge is  #
# rather poor, so it is sure that there are many things in this script which     #
# could have been done much better...                                            #
##################################################################################
# Script requires:                                                               #
# - ImageMagick >= 6.2.5                                                         #
# - uptimed                                                                      #
# - uprecords (uprecords-cgi)                                                    #
# - ncftp                                                                        #
# - scp                                                                          #
##################################################################################
# Contact? >> sufixx[at]gmail.com                                                #
##################################################################################


# Configurable variables
BASE_DIR=`pwd` # better to put here normal path!
OUT=$BASE_DIR/out.png
THEME="theme_gimpuj"
DEL_UPLOADED=0	# Set to 1 if you want to delete uploaded image from hard disk
#NETCHECK=1	# Uncomment to enable network check
#CHECKS=0	# Uncomment to disable software checks

# upload settings
UPLOAD="none"	# Choose method of image upload: ftp/scp/none
USER="sufixx"
PASSWORD="************"
HOST="ftp.com"
FTP_PORT=21
SSH_PORT=22
FTP_DIR="public_html/"
SSH_DIR="~/"


# No need to change
VERSION="0.55"
TMP_DIR=/tmp
THEME_DIR=$BASE_DIR/ # edit, to add custom dir theme
BACKGROUND=$THEME_DIR/$THEME/bg.png
FOREGROUND=$THEME_DIR/$THEME/fg.png

# Script command-line options

if [ "$1" == "-h" ]; then
	echo "Available options are:"
	echo " -h	Show this help"
	echo " -i	Print information about settings"
	echo " -n	Turn off software checks"
	echo " -s	Generate shutdown image"
	echo " -v	Print script version"
	exit
elif [ "$1" == "-n" ]; then
	CHECKS=0
	echo " [!] Running without software checks"
elif [ "$1" == "-v" ]; then
	echo " [i] SupTime script $VERSION"
	exit
elif [ "$1" == "-i" ]; then
	echo " Information about settings:"
	echo "Temponary directory:	$TMP_DIR"
	echo "Base directory:		$BASE_DIR"
	echo "Output file location:	$OUT"
	echo "Using theme:		$THEME"
	echo "Upload way:		$UPLOAD"
	echo "Upload user:		$USER"
	echo "Upload host:		$HOST"
	exit
fi

# Network connection checker
connection_check () {
	ping -c 1 google.com > /dev/null && NET=1
}

# Check connection if set
if [ "$NETCHECK" == "1" ]; then
	echo " [i] Checking network connection."
	connection_check
	if [ "$NET" != "1" ]; then
		echo " [-] No network connection"
		exit
	fi
fi

# Software checks.
if [ "$CHECKS" != "0" ]; then
	if [ ! -e /usr/bin/uprecords ]; then		# Check if uprecords is installed
		echo " [-] Please install uprecords program."
		exit
	fi
	IMAGEMAGICK=`identify -version | head -n 1 | awk {'print $3'}| sed 's/\.//' | sed 's/\.//'`

	if [ $IMAGEMAGICK -lt 625 ]; then		# Check if ImageMagick is installed
		echo " [-] Your ImageMagick version is older than 6.2.5"
		exit
	fi
fi

# Read current uptime from uprecords
if [ "$1" == "-s" ]; then
	connection_check
	if [ $NET == 1 ]; then
		UPTIME="shutdown"
		PID=`ps aux | grep suptimed.sh | head -n 1 | awk '{print $2}'`
		kill $PID && \
		echo " [i] Generating shutdown image"
	else
		echo " [-] No network connection"
		exit
	fi
	else
	UPTIME_DAYS=`uprecords -a | grep "\->" | awk '{print $3}'`
	if [ $UPTIME_DAYS == 1 ]; then
		UPTIME=`uprecords -a | grep "\->" | awk '{print $3 $4 $5 $6 }' | sed 's/\day/ day/' |sed 's/\,/ /'`
	else
		UPTIME=`uprecords -a | grep "\->" | awk '{print $3 $4 $5 }' | sed 's/\days/ days/' |sed 's/\,/ /'`
	fi
fi

# Read top uptime from uprecords
TOP_DAYS=`uprecords -a | head -n 3 | tail -n 1 | sed 's/\->//' | awk '{print $2}'`
if [ $TOP_DAYS == 1 ]; then
	TOP=`uprecords -a | head -n 3 | tail -n 1 | sed 's/\->//' | awk '{print $2 $3 $4 $5}' | sed 's/\day/ day/' |sed 's/\,/ /'`
else
	TOP=`uprecords -a | head -n 3 | tail -n 1 | sed 's/\->//' | awk '{print $2 $3 $4}' | sed 's/\days/ days/' |sed 's/\,/ /'`
fi

# Load theme configuration and text layout
if [ -e $THEME_DIR/$THEME/theme.conf ]; then
	THEME_LINES=`cat $THEME_DIR/$THEME/theme.conf  |wc -l`
	if [ "$THEME_LINES" -ge 5 ]; then
		cat $THEME_DIR/$THEME/theme.conf | head -n 3 > $TMP_DIR/theme_base.tmp
		cat $THEME_DIR/$THEME/theme.conf | grep "#FONT#" | sed 's/\#FONT# //' > $TMP_DIR/theme.tmp
		source $TMP_DIR/theme_base.tmp
		source $TMP_DIR/theme.tmp
		LAYOUTS_LOADED=0
		while [ "$LAYOUTS_LOADED" -le "$LAYOUTS" ]; do
			LAYOUTS_LOADED=$[$LAYOUTS_LOADED+1]
			cat $THEME_DIR/$THEME/theme.conf | grep "#LAYOUT$LAYOUTS_LOADED#" > $TMP_DIR/layout"$LAYOUTS_LOADED".tmp
			LAYOUTS_LINES=`cat $TMP_DIR/layout*.tmp| wc -l`
			if [ "$LAYOUTS_LINES" -ge 2 ]; then
				source $TMP_DIR/layout"$LAYOUTS_LOADED".tmp
			else
				echo " [-] Error in config file!"
				exit
			fi
		done
	else
		echo " [-] Error in config file!"
		exit
	fi
else
	echo " [-] Theme not found! Check if it really exists."
	exit
fi

# Render image with text from text file
cat $TMP_DIR/up1.txt | convert -background transparent -pointsize $FONT_SIZE -fill $FONT_COLOR -font $THEME_DIR/$THEME/$FONT label:@- -append $TMP_DIR/text1.png


LAYOUTS_MADE=0
while [ "$LAYOUTS_MADE" -lt "$LAYOUTS" ]; do
	LAYOUTS_MADE=$[$LAYOUTS_MADE+1]
	cat $TMP_DIR/up"$LAYOUTS_MADE".txt | convert -background transparent -pointsize $FONT_SIZE -fill $FONT_COLOR -font $THEME_DIR/$THEME/$FONT label:@- -append $TMP_DIR/text"$LAYOUTS_MADE".png

done

# Merge graphical background, text image and foreground 
cat $THEME_DIR/$THEME/theme.conf | grep "#COMPOSITE#" > $TMP_DIR/composite.tmp
source $TMP_DIR/composite.tmp

# Show size of generated image
KBYTES=`du -k $OUT | awk {'print $1'}`
echo " [i] Rendered image size is $KBYTES kB"

# Cleanup
rm -f $TMP_DIR/composite* $TMP_DIR/up*.txt $TMP_DIR/text*.png $TMP_DIR/theme*.tmp $TMP_DIR/extra*.tmp $TMP_DIR/layout*.tmp

# Upload image
if [ "$UPLOAD" == "ftp" ]; then
	ncftpput -u $USER -p $PASSWORD -P $FTP_PORT $HOST $FTP_DIR $OUT > /dev/null && \
	echo " [i] Image uploaded to ftp server"
elif [ "$UPLOAD" == "scp" ]; then
	scp -P $SSH_PORT $OUT $USER@$HOST:$SSH_DIR/ && \
	echo " [i] Image uploaded via scp"
fi

if [ $DEL_UPLOADED == 1 ]; then
	rm -f $OUT
fi

exit
